<template>
  <div class="app-container">
    <el-row>
      <el-col :span="24">
        <div class="h_top">
          <div class="bread_title">
            <span>新增设备</span>
          </div>
          <div class="save_button">
            <div class="buttonBox">
              <el-button type="primary" icon="el-icon-back" size="mini" @click="goBack">返回</el-button>
            </div>
            <div class="buttonBox">
              <el-button type="primary" icon="el-icon-document-checked" size="mini" @click="save">保存</el-button>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
    <div>
      <el-row>
        <el-col :span="24">
          <el-form ref="form" :model="data" :inline="true" :rules="formRules">
            <el-row :gutter="10">
              <el-col :xl="3" :lg="4" :md="6" :sm="8">
                <el-form-item prop="coding">
                  <div>编号</div>
                  <el-input v-model="data.coding" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="4" :md="6" :sm="8">
                <el-form-item prop="name">
                  <div>名称</div>
                  <el-input v-model="data.name" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :xl="4" :lg="4" :md="6" :sm="8">
                <el-form-item prop="brand">
                  <div>品牌</div>
                  <el-input v-model="data.brand" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :xl="4" :lg="4" :md="6" :sm="8">
                <el-form-item prop="spec">
                  <div>型号</div>
                  <el-input v-model="data.spec" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :xl="4" :lg="4" :md="6" :sm="8">
                <el-form-item prop="cost_id">
                  <div>成本中心</div>
                  <v-select v-model="data.cost_id" action="costList" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="4" :md="6" :sm="8">
                <el-form-item prop="weight">
                  <div>类别</div>
                  <el-select v-model="data.weight" placeholder="请选择" size="mini">
                    <el-option v-for="(item, index) in weight" :key="index" :label="item" :value="index" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="4" :md="6" :sm="8">
                <el-form-item prop="fun">
                  <div>资产性质</div>
                  <el-select v-model="data.fun" placeholder="请选择" size="mini">
                    <el-option v-for="(item, index) in fun" :key="index" :label="item" :value="index" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="3" :md="6" :sm="8">
                <el-form-item prop="status">
                  <div>状态</div>
                  <el-select v-model="data.status" placeholder="请选择" size="mini">
                    <el-option v-for="(item, index) in eqStatus" :key="index" :label="item" :value="index" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :xl="4" :lg="5" :md="6" :sm="8">
                <el-form-item prop="provider_id">
                  <div>供应商</div>
                  <v-select-s v-model="data.provider_id" action="onlyProviderList" />
                </el-form-item>
              </el-col>
              <el-col :xl="4" :lg="4" :md="6" :sm="8">
                <el-form-item prop="manufacturer_id">
                  <div>制造商</div>
                  <v-select-s v-model="data.manufacturer_id" action="onlyManufacturerList" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="4" :md="6" :sm="8">
                <el-form-item prop="serialize">
                  <div>出厂编号</div>
                  <el-input v-model="data.serialize" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="4" :md="6" :sm="8">
                <el-form-item prop="workshop">
                  <div>所属车间</div>
                  <v-select v-model="data.workshop" :disabled="!data.factory" :query="{factory_id:data.factory}" action="workshopList" @change="clearLine" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="4" :md="6" :sm="8">
                <el-form-item prop="line">
                  <div>所属生产线</div>
                  <v-select v-model="data.line" :disabled="!data.workshop" :query="{workshop_id:data.workshop}" action="lineList" @change="clearProcess" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="4" :md="6" :sm="8">
                <el-form-item prop="process">
                  <div>所属工位</div>
                  <v-select v-model="data.process" :disabled="!data.workshop" :query="{line_id:data.line}" action="processList" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="4" :md="6" :sm="8">
                <el-form-item prop="parent_id">
                  <div>第一级资产</div>
                  <v-select v-model="data.parent_id" :disabled="!data.process" :query="{position_id:data.process}" action="eqList" :is-top="true" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="4" :md="6" :sm="8">
                <el-form-item prop="parent_id">
                  <div>存放位置</div>
                  <el-input v-model="data.position" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="4" :md="6" :sm="8" hidden>
                <el-form-item prop="org">
                  <div>组织</div>
                  <v-select v-model="data.org" action="orgList" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="4" :md="6" :sm="8" hidden>
                <el-form-item prop="department">
                  <div>部门</div>
                  <v-select v-model="data.department" :disabled="!data.org" :query="{org:data.org}" action="departmentList" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="4" :md="6" :sm="8">
                <el-form-item prop="first_category">
                  <div>一级分类</div>
                  <v-select v-model="data.first_category" action="eqCategoryList" :query="{level:0}" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="4" :md="6" :sm="8">
                <el-form-item prop="category_id">
                  <div>终极分类</div>
                  <v-select v-model="data.category_id" action="eqCategoryList" :disabled="!data.first_category" :query="{parent_id:data.first_category}" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="3" :md="6" :sm="8">
                <el-form-item>
                  <div>生产日期</div>
                  <el-date-picker v-model="data.purchase_date" type="date" size="mini" value-format="yyyy-MM-dd" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="3" :md="6" :sm="8">
                <el-form-item>
                  <div>购置日期</div>
                  <el-date-picker v-model="data.manufacture_date" type="date" size="mini" value-format="yyyy-MM-dd" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="3" :md="6" :sm="8">
                <el-form-item>
                  <div>购置价格</div>
                  <el-input v-model="data.price" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="3" :md="6" :sm="8">
                <el-form-item prop="admin_id">
                  <div>操作者</div>
                  <v-select-s v-model="data.admin_id" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="3" :md="6" :sm="8">
                <el-form-item prop="sort">
                  <div>排序</div>
                  <el-input v-model="data.sort" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="3" :md="6" :sm="8">
                <el-form-item prop="used_group">
                  <div>使用岗位</div>
                  <v-select-s v-model="data.used_group" action="jobList" />
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="3" :md="6" :sm="8">
                <el-form-item prop="hb">
                  <div>环保设备</div>
                  <el-checkbox v-model="data.hb" true-label="1" false-label="0">环保设备</el-checkbox>
                </el-form-item>
              </el-col>
              <el-col :xl="3" :lg="3" :md="6" :sm="8">
                <el-form-item prop="hb">
                  <div>特种设备</div>
                  <el-checkbox v-model="data.special" true-label="1" false-label="0">特种设备</el-checkbox>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <el-tabs>
            <el-tab-pane label="描述">
              <el-row>
                <el-col :span="24">
                  <Tinymce id="content" v-model="data.content" />
                </el-col>
              </el-row>
            </el-tab-pane>
          </el-tabs>
        </el-col>
      </el-row>
    </div>
  </div>
</template>
<script>
import API from '@/api/asset/equipment'
import { weight, fun, eqStatus } from '@/utils/options'
import Tinymce from '@/components/Tinymce/index'
import VSelect from '@/components/select'
import VSelectS from '@/components/select-search'
export default {
  inject: ['addOne', 'goUrl'],
  components: {
    Tinymce,
    VSelect,
    VSelectS
  },
  data() {
    return {
      data: {
        coding: '',
        name: '',
        brand: '',
        spec: '',
        serialize: '',
        number: '',
        manufacture_date: '',
        install_date: '',
        purchase_date: '',
        guarantee: '',
        price: '',
        sort: 100,
        hb: false,
        special: false,
        position_id: 1,
        factory: 90,
        org: 1,
        department: 17
      },
      weight,
      fun,
      eqStatus,
      formRules: {
        name: [
          {
            required: true,
            message: '请输入名称',
            trigger: 'blur'
          }
        ],
        brand: [
          {
            required: true,
            message: '请输入品牌',
            trigger: 'blur'
          }
        ],
        spec: [
          {
            required: true,
            message: '请输入型号 ',
            trigger: 'blur'
          }
        ],
        cost_id: [
          {
            required: true,
            message: '请选择成本中心',
            trigger: 'change'
          }
        ],
        weight: [
          {
            required: true,
            message: '请选择类别',
            trigger: 'change'
          }
        ],
        fun: [
          {
            required: true,
            message: '请选择功能',
            trigger: 'change'
          }
        ],
        status: [
          {
            required: true,
            message: '请选择状态',
            trigger: 'change'
          }
        ],
        first_category: [
          {
            required: true,
            message: '请选择一级分类',
            trigger: 'change'
          }
        ],
        category_id: [
          {
            required: true,
            message: '请选择终极分类',
            trigger: 'change'
          }
        ],
        factory: [
          {
            required: true,
            message: '请选择所属工厂',
            trigger: 'change'
          }
        ],
        workshop: [
          {
            required: true,
            message: '请选择所属车间',
            trigger: 'change'
          }
        ],
        line: [
          {
            required: true,
            message: '请选择所属生产线',
            trigger: 'change'
          }
        ],
        process: [
          {
            required: true,
            message: '请输入所属工位',
            trigger: 'change'
          }
        ],
        sort: [
          {
            required: true,
            message: '请输入排序',
            trigger: 'blur'
          }
        ],
        used_group: [
          {
            required: true,
            message: '请选择使用岗位',
            trigger: 'change'
          }
        ]
      }
    }
  },
  watch: {
    workshop(val, oldVal) {}
  },
  methods: {
    save() {
      const thisData = this.data
      const thisForm = this.$refs.form
      this.addOne(API, thisData, thisForm)
    },
    weightFormat(row) {
      return this.getWeightFormat(row)
    },
    clearLine() {
      // this.data.line = null
    },
    clearProcess() {
      // this.data.process = null
    },
    goBack() {
      this.goUrl('/asset/equipment')
    }
  }
}
</script>

